﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using Luna小店.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.生产工单
{
    public partial class Frm上传CAD : DevExpress.XtraEditors.XtraForm
    {
        public String STYLE_ID { get; set; }//款式id
        public String STYLE_NAME { get; set; }//款式名称
        public String SIZE { get; set; }//尺寸
        public String SIZE_CD { get; set; }//尺寸CD
        public String STYLE_FIT_CD { get; set; }//版CD
        public Frm上传CAD()
        {
            InitializeComponent();
        }
        #region 选择款式
        private void searchLookUpEdit款式_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn款式_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }

        private void searchLookUpEdit款式View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit款式.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.STYLE_ID = this.searchLookUpEdit款式.Properties.View.GetRowCellValue(rowHandle, "StyleId").ToString();
                this.STYLE_NAME = this.searchLookUpEdit款式.Properties.View.GetRowCellValue(rowHandle, "StyleNameCn").ToString();
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn款式_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit款式.ToolTip = null;
            searchLookUpEdit款式.EditValue = null;
        }
        private void searchLookUpEdit款式_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.STYLE_ID + "  " + this.STYLE_NAME;
        }
        #endregion

        private void Frm上传CAD_Load(object sender, EventArgs e)
        {
            this.searchLookUpEdit款式.Properties.DataSource = StyleService.initStyleInfo("", 1);
            this.searchLookUpEdit尺寸.Properties.DataSource = SizeService.GetAllSize();

        }
        #region 选择尺寸
        private void searchLookUpEdit尺寸_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn尺寸_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }

        private void searchLookUpEdit尺寸View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit尺寸.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.SIZE = this.searchLookUpEdit尺寸.Properties.View.GetRowCellValue(rowHandle, "SIZE").ToString();
                this.SIZE_CD = this.searchLookUpEdit尺寸.Properties.View.GetRowCellValue(rowHandle, "SIZE_CD").ToString();
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn尺寸_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit尺寸.ToolTip = null;
            searchLookUpEdit尺寸.EditValue = null;
        }
        private void searchLookUpEdit尺寸_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.SIZE;
        }
        #endregion

        private void simpleButton上传_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (CADService.UploadFileStandard(ofd.FileName, Path.GetFileName(ofd.FileName), this.STYLE_FIT_CD, this.STYLE_ID, this.SIZE_CD) == true)
                {
                    MessageBox.Show("上传成功");
                }
                else
                {
                    MessageBox.Show("上传失败");
                }
            }
        }

        private void textBoxSTYLE_FIT_CD_TextChanged(object sender, EventArgs e)
        {
            this.STYLE_FIT_CD = textBoxSTYLE_FIT_CD.Text;
        }
    }
}
